package app;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import util.ip.Regex;

public class Category {
	
	Map<String, List<String>> allMap;
	int allMapSize;
	String[] head;
	List<String> category;
	Map<String, Integer> categoryMap = new HashMap<String, Integer>();
	Regex regex;
	int categoryNum;
	
	public Category(Map<String,List<String>> allMap) {
		int count = 0;
		regex = new Regex();
		this.allMap = allMap;
		allMapSize = allMap.size();
		categoryNum = allMapSize - 1;
		head = new String[categoryNum];
		for (String string : allMap.keySet()) {
			
			if (!string.equals("service")) {
				head[count] = string;
				count++;
				category = allMap.get(string);
				//System.out.println(category.size());
				for (int i = 0; i < category.size(); i ++) {
					categoryMap.put(category.get(i), 0);
				//	System.out.println(category.get(i));
				}
			}
			
		}
	}
	
	public String[] getHead() {
		return head;
	}
	
	public String getCategory(String domain) {
		String[] split = domain.split("\\.");
		int length = split.length;
		if (length < 3) { 
			return split[0];
		} else {
			return split[length - 3];
		}
	}
	
	public String getCategoryCode(List<String> domainList) {
		
		//String out = "";
		for (String string : categoryMap.keySet()) {
			categoryMap.put(string, 0);
		}
		
		for (String string : domainList) {
			if (!regex.isIpv4(string)) {
				if (categoryMap.containsKey(getCategory(string))) {
					categoryMap.put(getCategory(string), 1);
				}
			}
		}
		//System.out.print("输出的顺序为：");
		String eachCode = "";
		for (String string : head) {
			
			//System.out.print(string + ",");
			List<String> outList = allMap.get(string);
			
			int size = outList.size();
			
			
			for (int i = 0; i < size; i++) {
				if (categoryMap.get(outList.get(i)) != 0) {
					eachCode += 1;
				} else {
					eachCode += 0;
				}
			}
			eachCode += ",";
		}
		//System.out.println("\n");

		
		return eachCode;
		
	}
	
	public int getCategoryNum() {
		return categoryNum;
	}
	
	public Map<String, Integer> getCategoryMap() {
		return categoryMap;
	}
	

	
	
	

}
